package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/dialect"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	localmixin "gitee.com/wenchangshou/simple-admin-pancakebot-rpc/ent/schema/localmixin"
	types2 "gitee.com/wenchangshou/simple-admin-pancakebot-rpc/ent/types"
	"github.com/suyuan32/simple-admin-common/orm/ent/mixins"
)

type Device struct {
	ent.Schema
}

func (Device) Fields() []ent.Field {
	return []ent.Field{
		field.String("device_name").
			Annotations(entsql.WithComments(true)).
			Comment("设备名称"),
		field.Int("device_no").
			Annotations(entsql.WithComments(true)).
			Comment("设备编号"),

		field.Int("device_status").
			Annotations(entsql.WithComments(true)).
			Comment("设备状态"),

		field.Time("last_heartbeat").Optional().Nillable().
			Annotations(entsql.WithComments(true)).
			Comment("最后心跳时间"),
		field.Int("type").Optional().Default(0),
		field.String("ext").Default("{}"),
		field.String("address"),

		// 出餐口数组,默认3个出餐口
		field.JSON("outlet_ports", []types2.OutletPort{}).
			Default([]types2.OutletPort{
				{Number: 1, InUse: false, Status: 0},
				{Number: 2, InUse: false, Status: 0},
				{Number: 3, InUse: false, Status: 0},
			}).
			Annotations(entsql.WithComments(true)).
			Comment("出餐口列表"),

		field.Other("location", &types2.GeoJson{}).
			SchemaType(map[string]string{dialect.Postgres: "GEOMETRY(Point, 4326)"}),
	}
}
func (Device) Mixin() []ent.Mixin {
	return []ent.Mixin{
		mixins.IDMixin{},
		localmixin.SoftDeleteMixin{},
	}

}

// Edges of the Device.
func (Device) Edges() []ent.Edge {
	return []ent.Edge{
		// Many-to-Many: a device can have many combos.
		edge.To("combos", Combo.Type),
	}
}
